package com.cmpe451.nutty.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.cmpe451.nutty.dao.UserDao;
import com.cmpe451.nutty.model.AppUrl;
import com.cmpe451.nutty.model.ExampleModel;
import com.cmpe451.nutty.model.Follow;
import com.cmpe451.nutty.model.Levels;
import com.cmpe451.nutty.model.Privilege;
import com.cmpe451.nutty.model.Report;
import com.cmpe451.nutty.model.Role;
import com.cmpe451.nutty.model.User;
import com.cmpe451.nutty.so.GuiPrivilege;
import com.cmpe451.nutty.so.GuiUser;

public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//      User user = userDao.findUserByUsername(username);
//      if (user == null) {
//          user = userDao.findUserByEmail(username);
//      }
//
//      if (user == null) {
//          throw new UsernameNotFoundException("error.username.notfound");
//      }
        
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userDao.findUserByUsername(username);

        if (user == null) {
            return null;
        }

        GuiUser guiUser = convertUserToGuiUser(user);
        return guiUser;
    }

    @Override
    public List<AppUrl> getAllSecureUrls() {
        return userDao.findAllSecureUrls();
    }

    @Override
    public GuiUser getGuestUser() {
        User user = userDao.findUserByUsername("guest");
        return convertUserToGuiUser(user);
    }

    private GuiUser convertUserToGuiUser(User user) {
        GuiUser guiUser = new GuiUser();
        guiUser.setPassword(user.getPassword());
        guiUser.setUsername(user.getUsername());
        guiUser.setEmail(user.getEmail());
        guiUser.setLevel_point(user.getLevel_point());
        Levels l=userDao.findLevelByPoint(user.getLevel_point());
        guiUser.setTitle(l.getLevel_name());
        guiUser.setLevel_max_point(l.getMax_val());
        guiUser.setLevel_min_point(l.getMin_val());
        guiUser.setHometown(user.getHometown());
        guiUser.setId(user.getId());
        
        for (Role role : user.getRoles()) {
            for (Privilege priv : role.getPrivileges()) {
                guiUser.getAuthorities().add(new GuiPrivilege(priv.getName()));
            }
        }

        return guiUser;
    }
    
    private GuiUser convertUserToGuiUser(ExampleModel user) {
        GuiUser guiUser = new GuiUser();
        guiUser.setPassword(user.getPassword());
        guiUser.setUsername(user.getUsername());
        guiUser.setId(user.getId());
        return guiUser;
    }

    @Override
    public List<GuiUser> getUsers(int page, int max, String orderBy, String orderType) {
        List<User> users = userDao.findUsers(page, max, orderBy, orderType);
        List<GuiUser> results = new ArrayList<GuiUser>();
        for (User user : users) {
            results.add(convertUserToGuiUser(user));
        }
        return results;
    }

    @Override
    public GuiUser getGuiUserById(long id) {
        User user = userDao.findUserById(id);
        GuiUser guiUser = null;
        if (user != null) {
            guiUser = convertUserToGuiUser(user);
        }
        return guiUser;
    }

    @Override
    public User getUserById(long id) {
        return userDao.findUserById(id);
    }
    
    @Override
    public User getUserByName(String username){
    	return userDao.findUserByUsername(username);
    }

    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
    }

        @Override
        public Role getRoleByName(String string) {
                return userDao.findRoleByName(string);
        }

        @Override
        public void saveFollow(Follow follow) {
                // TODO Auto-generated method stub
                
        }

        @Override
        public List<User> findSearchResultsofUsers(String q) {
                // TODO Auto-generated method stub
                return userDao.getUsersBySearch(q);
        }

        @Override

        public User getUserByEmail(String email) {
        	return userDao.findUserByEmail(email);
        }

		@Override
		public void saveReport(Report report) {
			
			userDao.saveReport(report);
			
		}

		@Override
		public Report checkReport(String username, String username2) {
			// TODO Auto-generated method stub
			return userDao.checkReported(username, username2);
		}

		@Override
		public int checkReportCount(String username) {
			// TODO Auto-generated method stub
			return userDao.getReportCount(username);
		}

		@Override
		public List<Report> getReportedOnes(String username2) {
			// TODO Auto-generated method stub
			return userDao.getReportedOnes(username2);
		}

		@Override
		public Levels getTitle(Long levelPoint) {
			// TODO Auto-generated method stub
			return userDao.getTitle(levelPoint);
		}

		@Override
		public void sendRecoveryMail(String username, String email) {
			// TODO Auto-generated method stub
			
			userDao.sendRecoveryMail(username, email);
		}
}